import request from "@/utils/request";
import store from "@/store";
import {getYsdApiData} from "@/views/youshuda/api/BaseApi";

const sysInfo = {
  state: {
    sysName: '',
    sysLogo: '',
    sysPhone: '',
    sysMaxExportCount: '',
    enabledSysWatermark:'',
    sysWatermarkApiCode:'',
    sysWatermark:'',
    remark:'',
    YSD_BASE_URL:'',
    RESOURCE_PREFIX:''
  },

  mutations: {
    SET_SYS_NAME: (state, sysName) => {
      state.sysName = sysName
    },
    SET_SYS_LOGO: (state, sysLogo) => {
      state.sysLogo = sysLogo
    },
    SET_SYS_PHONE: (state, sysPhone) => {
      state.sysPhone = sysPhone
    },
    SET_SYS_MAX_EXPORT_COUNT: (state, sysMaxExportCount) => {
      state.sysMaxExportCount = sysMaxExportCount
    },
    SET_ENABLED_SYS_WATERMARK: (state, enabledSysWatermark) => {
      state.enabledSysWatermark = enabledSysWatermark
    },
    SET_SYS_WATERMARK_APICODE: (state, sysWatermarkApiCode) => {
      state.sysWatermarkApiCode = sysWatermarkApiCode
    },
    SET_SYS_WATERMARK: (state, sysWatermark) => {
      state.sysWatermark = sysWatermark
    },
    SET_REMARK: (state, remark) => {
      state.remark = remark
    },
    SET_YSD_BASE_URL: (state, YSD_BASE_URL) => {
      state.YSD_BASE_URL = YSD_BASE_URL
    },
    SET_RESOURCE_PREFIX: (state, RESOURCE_PREFIX) => {
      state.RESOURCE_PREFIX = RESOURCE_PREFIX
    }
  },

  actions: {
    // 获取系统信息
    GetSysInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        request({
          url: '/system/SysInfo',
          method: 'get'
        }).then(res => {
          if(res && res.data){
            const sysInfo = res.data
            const logo = (sysInfo.logo == "" || sysInfo.logo == null) ? require("@/assets/logo/logo.png") : process.env.VUE_APP_BASE_API + sysInfo.logo;
            const sysName = sysInfo['name'] || process.env.VUE_APP_TITLE
            let sysWatermarkApiCode = sysInfo['sysWatermarkApiCode'] || 'YsdBaseSysWatermark'
            commit('SET_SYS_LOGO', logo)
            commit('SET_SYS_NAME', sysName)
            commit('SET_SYS_PHONE', sysInfo['phone'])
            commit('SET_SYS_MAX_EXPORT_COUNT', sysInfo['sysMaxExportCount'] || 20000)
            commit('SET_ENABLED_SYS_WATERMARK', sysInfo['enabledSysWatermark'])
            commit('SET_SYS_WATERMARK_APICODE', sysWatermarkApiCode)
            commit('SET_REMARK', sysInfo['remark'])
            commit('SET_YSD_BASE_URL', sysInfo['YSD_BASE_URL'])
            commit('SET_RESOURCE_PREFIX', sysInfo['RESOURCE_PREFIX'])
            /**
             * 当用户登录成功后，获取系统水印
             */
            if(sysInfo['enabledSysWatermark'] === '1' && store.getters.name){
              // 请求优树搭接口数据
              getYsdApiData(sysWatermarkApiCode, {
                'sysName': sysName
              }).then(res => {
                // res为优树搭API返回信息
                if (res.code != 200) {
                  this.msgError("系统水印获取异常："+res.msg)
                  return;
                }
                commit('SET_SYS_WATERMARK', res.data)
              })
            }
          }else{
            commit('SET_SYS_LOGO', require("@/assets/logo/logo.png"))
          }
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    }

  }
}

export default sysInfo
